package com.dayatang.exam.domain;

import java.util.Map;
import java.util.TreeMap;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;

import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.SortType;

/**
 * 单�?�?
 * 
 * @author yang
 * 
 */
@Entity
@DiscriminatorValue("SC")
public class SingleChoiceQuestion extends Question implements ObjectiveQuestion {
	
	private static final long serialVersionUID = -2115703999982112311L;

	/**
	 * 选择�?
	 */
	@org.hibernate.annotations.CollectionOfElements
	@JoinTable(name = "QUESTION_OPTIONS", joinColumns = @JoinColumn(name = "QUESTION_ID"))
	@org.hibernate.annotations.MapKey(columns = @Column(name = "CODE", nullable = false))
	@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	@org.hibernate.annotations.Sort(type = SortType.NATURAL)
	private Map<String, QuestionOption> options = new TreeMap<String, QuestionOption>();
	
	public Map<String, QuestionOption> getOptions() {
		return options;
	}

	public void setOptions(Map<String, QuestionOption> options) {
		this.options = options;
	}
	
	public void addOption(String key, QuestionOption option) {
		options.put(key, option);
	}

	@Override
	public String getTypeName() {
		return Question.TYPE_SINGLE_CHOICE;
	}

	public boolean isCorrect(String answer) {
		return getSolution().equals(answer);
	}
	
	public static SingleChoiceQuestion get(Long id) {
		return getRepository().get(SingleChoiceQuestion.class, id);
	}
}
